; (function ($) {

  const dropdown = '[data-plugin="select"]'
  const selector = '.text'

  const UiSelect = function (element) {  
    this.$element = $(element)
    this.$select = $(element).find('select')
    this.$text = $(element).find('.text')
    this.$ul = $(element).find('>.ui-selection-options')
    this.name = this.$select.attr('name')
    this.value =   

    this.setValue()
    this.$element.on('click', selector, $.proxy(this.toggle, this))
    this.$ul.on('click', '>li', $.proxy(this.select, this))
    $(document).on('click', function(e){
      let target = e.target
      if(target !== element) clearAll()
    })
  }


  UiSelect.prototype.toggle = function (e) {
    clearAll(this.$element)
    e.stopPropagation()
    this.$element.hasClass('active') ? this.hide() : this.show()
  }

  UiSelect.prototype.hide = function(){
    this.$element.removeClass('active')
  }

  UiSelect.prototype.show = function(){
    this.$element.addClass('active')
  }

  UiSelect.prototype.select = function(event){
    event.stopPropagation()
    let $ele = $(event.currentTarget)
    if($ele.hasClass('header')) return
    $ele.addClass('selected').siblings().removeClass('selected')
    this.setValue()
  }

  UiSelect.prototype.setValue = function (e) {
    let $selected = this.$ul.find('li.selected')
    let value = $selected.data('value')
    let text = $selected.text()
    let target = {
      relatedTarget: $selected.get(0),
      selectName: this.name,
      value: value
    }
    this.$select.find('option').each(function (i, ele) {
      $(ele).attr('selected', $(ele).val() == value)
    })
    this.$text.text(text)
    this.value = value
    this.$element.trigger($.Event('onSelect', target))
    this.hide()
  }

  $.fn.uiSelect = function () {
    return this.each(function () {
      new UiSelect(this)
    })
  }

  function clearAll($element) {
    $(dropdown).not($element).each(function () {
      $(this).removeClass('active')
    })
  }

  $(document).ready(function () {
    $(dropdown).uiSelect()
  })
})(jQuery)

module.exports = function(){
  
}